#include <bits/stdc++.h>
// 2025/07/20
// tag:
// Author: Forever
using namespace std;
using int64 = int64_t;
#define int long long
#define MOD 1000000007
string s;
char st1[101];
int top1, top2, st2[101];
vector<int> v;

int help(char op)
{
    if (op == '+' || op == '-')
        return 1;
    if (op == '*' || op == '/')
        return 2;
    if (op == '^')
        return 3;
    return 0;
}

bool check(string s){
    int l = 0, r = 0;
    for (int i = 0; i < s.size(); i++) {
        if (s[i] == '(')
            l++;
        else if (s[i] == ')')
            r++;
        if (l < r)
            return false;
    }
    return l == r;
}

int pow(int a, int b){
    int res = 1;
    while (b){
        if (b & 1)
            res = res * a % MOD;
        a = a * a % MOD;
        b >>= 1;
    }
    return res;
}

int solve(string s){
    int n = s.size(), x = 0;
    top1 = 0, top2 = 0;
    v.clear();
    for (int i = 0; i < n; i++) {
        if (isdigit(s[i])) {
            x = x * 10 + s[i] - '0';
        } else if (s[i] == 'a') {
            v.push_back(1145);
        } else if (s[i] == '(') {
            st1[++top1] = s[i];
        } else if (s[i] == ')') {
            while (top1 > 0 && st1[top1] != '(') {
                v.push_back(MOD + st1[top1]);
                top1--;
            }
            if (top1 > 0 && st1[top1] == '(') {
                top1--;
            }
        } else {
            while (top1 > 0 && st1[top1] != '(' && (help(s[i]) <= help(st1[top1]))) {
                v.push_back(MOD + st1[top1]);
                top1--;
            }
            st1[++top1] = s[i];
        }
        if ((i == n - 1 || !isdigit(s[i + 1])) && isdigit(s[i])) {
            v.push_back(x % MOD);
            x = 0;
        }
    }
    while (top1 > 0) {
        v.push_back(MOD + st1[top1]);
        top1--;
    }
    for (int i = 0; i < v.size(); i++) {
        if (v[i] < MOD) {
            st2[++top2] = v[i];
        } else {
            int a = st2[top2--];
            if (top2 == 0){
                if (v[i] == '+' + MOD)
                    st2[++top2] = a;
                else if (v[i] == '-' + MOD)
                    st2[++top2] = -a;
            }
            else {
                int b = st2[top2--];
                if (v[i] == '+' + MOD)
                    st2[++top2] = b + a;
                else if (v[i] == '-' + MOD)
                    st2[++top2] = b - a;
                else if (v[i] == '*' + MOD)
                    st2[++top2] = b * a;
                else if (v[i] == '/' + MOD)
                    st2[++top2] = b / a;
                else if (v[i] == '^' + MOD)
                    st2[++top2] = pow(b, a);
                
            }
            st2[top2] = (st2[top2] + MOD) % MOD;
        }
    }
    return st2[top2];
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    string s;
    char c = getchar();
    while (c != '\n' && c != '\r') {
        if (c != ' ') {
            s += c;
        }
        c = getchar();
    }
    int n = 0;
    while (c == '\n' || c == '\r') {
        c = getchar();
    }
    while (c != '\n' && c != '\r') {
        n = n * 10 + c - '0';
        c = getchar();
    }
    int ans = solve(s);
    for (int i = 1; i <= n; i++) {
        s = "";
        c = getchar();
        while (c == '\n' || c == '\r') {
            c = getchar();
        }
        while (c != '\n' && c != '\r') {
            if (c != ' ') {
                s += c;
            }
            c = getchar();
        }
        if (check(s) && solve(s) == ans) {
            cout << char('A' + i - 1);
        }
    }
    return 0;
}